package com.gcsoftware.web.service.elasticsearch;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gcsoftware.configs.AppConstants;
import com.gcsoftware.web.beans.elasticsearchData.Agg;
import com.gcsoftware.web.beans.elasticsearchData.Bucketz;
import com.gcsoftware.web.beans.returnPage.*;
import com.gcsoftware.web.mapper.T_GW_DSITEMMAPPER;
import com.gcsoftware.web.mapper.elasticsearch.HomeDao;
import com.gcsoftware.web.mapper.elasticsearch.ReportDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import static com.gcsoftware.configs.AppConstants.Elasticsearch.ES_MATCH_PHRASE_TAG;
import static com.gcsoftware.configs.AppConstants.Elasticsearch.ES_SENSITIVE_ITEM_NAMES;

/**
 * 首页
 * @author czp
 * date : 2018-04-11
 */
@Service
public class HomeService {
    @Autowired
    private HomeDao homeDao;

    @Autowired
    private ReportDao reportDao;

    @Autowired
    private T_GW_DSITEMMAPPER appDao;

    public List<ProtectItem> sensitiveItemPie(String range) {
        return appDao.protectItemsGroupBy();
    }

    public Buckets[] sensitiveItemTopBar(String range, String type) {
        Integer top = Integer.parseInt(AppConstants.Sys.SYS_SENSITIVE_ITEM_TOP.getValue());
        List<String> sensitiveItems = appDao.sensitiveItems();
        int bufSize = sensitiveItems.size();
        if (bufSize != 0) {
            Buckets[] buckets = new Buckets[bufSize];
            for (int i = 0; i < bufSize; i++) {
                String itemName = sensitiveItems.get(i);
                long count = reportDao.asensitiveItemCount(itemName, ES_SENSITIVE_ITEM_NAMES.getValue(),
                        ES_MATCH_PHRASE_TAG.getValue(), range);
                buckets[i] = new Buckets(itemName, count);
            }
            Arrays.sort(buckets, new BucketsComparator());
            if (bufSize > top && !type.equals("PIE")) {
                Buckets[] rBuckets = new Buckets[top];
                for (int i = 0; i < top; i++) rBuckets[i] = buckets[i];
                return rBuckets;
            } else {
                return buckets;
            }
        } else {
            return null;
        }
    }

    public AppCountBar apiCountBar(String range, String appName, String apiName) {
        Long temp;
        List<String> appNames = appDao.getApps();
        List<String> apiNames = appDao.getApis();
        Map<String, List<Long>> rMap = new HashMap<>();
        if (apiNames.size() != 0 && apiNames.size() != 0) {
            Agg<Buckets> esGroup = (Agg<Buckets>) homeDao.apiCountBar(range, appName, apiName);
            ObjectMapper mapper = new ObjectMapper();
            if (esGroup == null) return null;
            for (int i = 0; i < esGroup.getBuckets().size(); i++) { // appNames
                Bucketz bucketz = mapper.convertValue(esGroup.getBuckets().get(i), Bucketz.class);
                List<Long> rLong = new ArrayList<>();
                for (int j = 0; j < apiNames.size(); j++) { // 基准
                    temp = 0L;
                    for (int k = 0; k < bucketz.getEs_api_name_groups().getBuckets().size(); k++) { // apiNames
                        Buckets buckets = mapper.convertValue(bucketz.getEs_api_name_groups().getBuckets().get(k), Buckets.class);
                        if (buckets.getKey().equals(apiNames.get(j))) {
                            temp = buckets.getDoc_count();
                            break;
                        }
                    }
                    rLong.add(temp);
                }
                rMap.put(bucketz.getKey(), rLong);
            }
            return new AppCountBar(appNames, apiNames, rMap);
        } else {
            return null;
        }
    }

    public HomeLabelData labelData(String range) {
        long interfaceUsage = homeDao.interfaceUsage(range);
        int interfaceCount = appDao.interfaceCount();
        int clientApp = appDao.clientApp();
        int protectItem = appDao.protectItemCount();
        return new HomeLabelData(interfaceUsage, interfaceCount, clientApp, protectItem);
    }
}
